package com.example.demo.controller;

import com.example.demo.common.JwtUtils;
import com.example.demo.common.ResultModel;
import com.example.demo.common.ResultModelTool;
import com.example.demo.entity.Notice;
import com.example.demo.entity.Public_info;
import com.example.demo.entity.User;
import com.example.demo.service.NoticeService;
import com.example.demo.service.Public_infoService;
import com.example.demo.service.UserService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Positive;
import java.util.List;
@CrossOrigin
@Controller
@RequestMapping("/public_infos")
@Transactional
public class Public_infoController {


    @Resource
    Public_infoService public_infoService;

    @Resource
    private UserService userService;
    @Autowired
    HttpServletRequest request;
    @Autowired
    private JwtUtils jwtUtils;


    /**
     * 通知新增
     * @param public_info
     * @return
     */
    @PostMapping("")
    @ResponseBody

    public ResultModel insert(@RequestBody Public_info public_info){

        String token = request.getHeader("Authorization");
        Claims claims = JwtUtils.parseJwt(token);
        String userId = claims.getId();
        User user1=userService.getUser(userId);
        if(user1==null){
            return new ResultModel(200,"权限不足或者token过期");
        }
        ResultModel resultmodel=new ResultModel();
        resultmodel.setCode(200);
        public_infoService.insertnotice(public_info);
        resultmodel.setData("添加成功");
        return ResultModelTool.handleResultModel(resultmodel);
    }

    /**
     * 通知列表
     * @return
     */
    @GetMapping("")
    @ResponseBody
    public ResultModel selectAll(){
        List<Public_info> list=public_infoService.selectAll();
        ResultModel resultModel=new ResultModel();
        resultModel.setCode(200);
        resultModel.setData(list);
        return ResultModelTool.handleResultModel(resultModel);
    }

    /**
     * 单个通知查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ResponseBody
    public ResultModel selectid(@PathVariable(value = "id") @Positive(message = "{id}") int id){



        ResultModel resultModel=new ResultModel();
        resultModel.setCode(200);
        resultModel.setData(public_infoService.selectid(id));
        return ResultModelTool.handleResultModel(resultModel);
    }

    /**
     * 通知修改
     * @param public_info
     * @return
     */
    @PutMapping ("")
    @ResponseBody
    public ResultModel updateid( @RequestBody Public_info public_info){

        String token = request.getHeader("Authorization");
        Claims claims = JwtUtils.parseJwt(token);
        String userId = claims.getId();
        User user1=userService.getUser(userId);
        if(user1==null){
            return new ResultModel(200,"权限不足或者token过期");
        }
        ResultModel resultModel=new ResultModel();
        resultModel.setCode(200);
        public_infoService.updateid(public_info);
        resultModel.setData("修改成功");
        return ResultModelTool.handleResultModel(resultModel);
    }

    /**
     * 通知删除
     * @param id
     * @return删除时为2 没删除时为1
     */

    @DeleteMapping("/{id}")
    @ResponseBody
    public ResultModel deleteid(@PathVariable(value = "id") @Positive(message = "{id}") int id){

        String token = request.getHeader("Authorization");
        Claims claims = JwtUtils.parseJwt(token);
        String userId = claims.getId();
        User user1=userService.getUser(userId);
        if(user1==null){
            return new ResultModel(200,"token过期或者权限不足");
        }
//        public_infoService.deleteid(id);
        ResultModel resultModel=new ResultModel();
        resultModel.setCode(200);
        public_infoService.deleteid(id);
        resultModel.setData("删除成功");
        return ResultModelTool.handleResultModel(resultModel);

    }

}
